#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "KeywordLineEdit.h"

#define EDITKW0 0
#define EDITKW1 1
#define EDITKW2 2

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    , check1CaseSensitive(false)
    , check1WholeWords(false)
    , check1RegularExpression(false)
    , check2CaseSensitive(false)
    , check2WholeWords(false)
    , check2RegularExpression(false)
  , keywordEditUsed_(EDITKW0)
{
    ui->setupUi(this);
    additionalUISet();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    onRelocateRightSide();
}

void MainWindow::additionalUISet()
{
    ui->widgetAdvance->setVisible(false);
    ui->WidgetRightSide->setVisible(false);
    connect(ui->editKeyword1,&KeywordLineEdit::sgFocusEvent,
            [this](bool trueIsFocusIn){onInputFocus(EDITKW1,trueIsFocusIn);});
    connect(ui->editKeyword2,&KeywordLineEdit::sgFocusEvent,
            [this](bool trueIsFocusIn){onInputFocus(EDITKW2,trueIsFocusIn);});
    connect(ui->btnAdvance,&QPushButton::toggled,
            [this](bool toggled){
       if(toggled == false){
           QString bkStyle;
           ui->editKeyword1->setStyleSheet(bkStyle);
           ui->editKeyword2->setStyleSheet(bkStyle);
           ui->widgetAdvance->setStyleSheet(bkStyle);
           onAdvanceLoad(EDITKW0);
       }
    });
    connect(ui->checkCaseSensitive,&QCheckBox::toggled,
            [this](bool toggled){
       if(keywordEditUsed_ == EDITKW1) check1CaseSensitive = toggled;
       if(keywordEditUsed_ == EDITKW2) check2CaseSensitive = toggled;
    });
    connect(ui->checkWholeWordsOnly,&QCheckBox::toggled,
            [this](bool toggled){
       if(keywordEditUsed_ == EDITKW1) check1WholeWords = toggled;
       if(keywordEditUsed_ == EDITKW2) check2WholeWords = toggled;
    });
    connect(ui->checkRegularExpression,&QCheckBox::toggled,
            [this](bool toggled){
       if(keywordEditUsed_ == EDITKW1) check1RegularExpression = toggled;
       if(keywordEditUsed_ == EDITKW2) check2RegularExpression = toggled;
    });
}

void MainWindow::onInputFocus(int numb, bool trueIsFocusIn)
{
    if(ui->btnAdvance->isChecked())
    {
        if(trueIsFocusIn){
            QString bkStyle;
            Q_ASSERT(numb == EDITKW1 || numb == EDITKW2);
            if(numb==1){
                bkStyle = "background-color: #ccddff";
                ui->editKeyword1->setStyleSheet(bkStyle);
                ui->editKeyword2->setStyleSheet(QString());
                ui->widgetAdvance->setStyleSheet(bkStyle);
                onAdvanceLoad(EDITKW1);
            }
            if(numb==2){
                bkStyle = "background-color: #ddffcc";
                ui->editKeyword1->setStyleSheet(QString());
                ui->editKeyword2->setStyleSheet(bkStyle);
                ui->widgetAdvance->setStyleSheet(bkStyle);
                onAdvanceLoad(EDITKW2);
            }
        }
    }
}

void MainWindow::onAdvanceLoad(int numb)
{
    keywordEditUsed_ = numb;
    switch(numb)
    {
    case EDITKW0:
        ui->checkCaseSensitive->setChecked(false);
        ui->checkWholeWordsOnly->setChecked(false);
        ui->checkRegularExpression->setChecked(false);
        ui->WidgetRightSide->setVisible(true);
        break;
    case EDITKW1:
        ui->checkCaseSensitive->setChecked(check1CaseSensitive);
        ui->checkWholeWordsOnly->setChecked(check1WholeWords);
        ui->checkRegularExpression->setChecked(check1RegularExpression);
        break;
    case EDITKW2:
        ui->checkCaseSensitive->setChecked(check2CaseSensitive);
        ui->checkWholeWordsOnly->setChecked(check2WholeWords);
        ui->checkRegularExpression->setChecked(check2RegularExpression);
        break;
    default:
        Q_ASSERT(false);
    }
}

void MainWindow::onRelocateRightSide()
{
    QSize sz = ui->listWidget->size();
    ui->WidgetRightSide->setGeometry(QRect(sz.width()-180,0,180,sz.height()));
}
